import options from './request.js';
import * as hotelApi from './hotel';
import * as addressApi from './address';
import * as myOrderApi from './myOrder';
import * as homeApi from './home';
import * as carRentalApi from './carRental';
import * as dictApi from './dict';
import * as classificationApi from './classification';
import {
	Socket
} from './socket';
const SET_END_TIME = ()=>{
	let date = new Date()
	date.setDate(date.getDate() + 10);
	uni.setStorageSync('end_time', date.getTime()); //设置过期时间
}


const METHODS = {
	login() { //登录
		/*************  时间到清空本地缓存 **************/
		let endTime = uni.getStorageSync('end_time');
		if (endTime) {
			if (endTime <= Date.now()) {
				uni.clearStorageSync();
			}else{
				SET_END_TIME(); //追加时间
			}
		} else {
			uni.clearStorageSync();
		}
		/************* 时间到清空本地缓存 **************/
		return new Promise((relove, reject) => {
			let socket;
			uni.login({
				success: res => {
					uni.request({
						url: `${options.realmName}/viewLogin/user/login`,
						method: "GET",
						data: {
							code: res.code
						},
						success: res => {
							if (res.data && res.data.data) {
								if (!res.data.data.phone) { //如果没有手机号删除本地获取手机号属性
									uni.setStorageSync('userInfo', '');
								};
								uni.setStorageSync("Authorization", res.data.data
									.token);
								uni.setStorageSync("openid", res.data.data
									.openid_str);
								uni.setStorageSync('power', res.data.data.limit ||
									'1');
								uni.setStorageSync('phone', res.data.data.phone);
								if (res.data.data.phone) {
									let userInfo = {
										"avatarUrl": "",
										"city": "",
										"country": "",
										"gender": 0,
										"language": "zh_CN",
										"nickName": "微信用户",
										"province": "",
										"phone": res.data.data.phone
									};
									uni.setStorageSync('userInfo',userInfo);
								};
								console.log('当前人的权限：',res.data.data.limit)
								socket = new Socket(res.data.data.openid_str);
								relove(res);
							}
						}
					})
				}
			});
		})
	},
	getPhone(data) { //获取手机号
		SET_END_TIME();
		return options.reqForm('GET', '/viewLogin/user/phoneLogin', data);
	},
	WX_PAY(data, callBack) {
		this.weixinPay(data).then(res => {
			let payData = res.data.data
			uni.getProvider({
				service: "payment",
				success: res => {
					uni.requestPayment({
						"timeStamp": payData.timestamp, //当前时间
						"nonceStr": payData.noncestr,
						"package": payData.package,
						"signType": payData.signType,
						"paySign": payData.sign,
						success: sres => {
							console.log('支付成功', res);
							console.log('微信支付返回', sres)
							uni.hideLoading()
							if (callBack) {
								callBack(payData.orderNo, 'yes');
							}
						},
						fail: err => {
							console.log('支付失败', err);
							uni.hideLoading()
							if (callBack) {
								callBack(payData.orderNo, 'no');
							}
						}
					});
				}
			});
		})
	},
	...hotelApi,
	...addressApi,
	...myOrderApi,
	...homeApi,
	...carRentalApi,
	...dictApi,
	...classificationApi,
	Socket,
	realmName: options.realmName,
	payCallback(data) { // jd
		return options.reqJson('POST', `/third/payCallback`, data);
	},
	getjdurl(data) { // jd
		return options.reqJson('POST', `/jd/login/index`, data);
	},
	getuselist(data) { // 景区门票产品列表
		data.onShowType = 0;
		return options.reqJson('GET', `/clota/product/weChartList`, data).then(res => {
			res.data.rows = res.data.rows.map(item => {
				item.productName = item.productName.replaceAll('OTA', '');
				return item;
			});
			return Promise.resolve(res)
		});
	},
	getushow(data) { // 创建订单
		return options.reqJson('POST', `/ticOrder/createTicOrder`, data);
	},

	getushowping(data) { // 根据订单编号获取明细【移动端】
		return options.reqJson('GET', `/ticOrder/getTicOrderPageMobile`);
	},
	getushowping(data) { // 根据电⼦串码获取⼊园凭证url
		return options.reqJson('GET', `/ticOrder/getQrCode`);
	},
	dunnerNumber(data) { // 获取信息
		return options.reqJson('GET', `/viewLogin/user/dunnerNumber`);
	},
	getTicketOrderPay(data) { //获取支付参数拉起微信支付
		return options.reqJson('POST', `/third/pay`, data);
	},
	setTicketPaySuccess(data) { //支付成功后回调
		return options.reqJson('POST', `/third/payCallback`, data);
	},
	heatList(data) { //热门活动
		return options.reqJson('GET', `/restaurant/config/weChatList`, data);
	},
	Meeting(data) { //会议场地
		return options.reqJson('GET', `/meeting/venue/list`, data);
	},

}

Object.keys(METHODS).forEach(item => {
	exports[item] = METHODS[item]
})
